import { commons, system, user } from '@/api'

const state = {
  provinces: [],
  citys: [],
  countys: [],
  dProvinces: [],
  dCitys: [],
  dCountys: [],
  tab_key: '',
  roleSelectList: [],
  pointData: {
    sectionName: '',
    goodsTable: [],
    classifyTable: [],
    outsideType: ''
  },
  // 是否在请求中
  isAppending: false,
  // 企业主资料刷新开票记录列表
  etpRefreshInvocieLog: false,
  // 企业主资料刷新基础信息
  etpRefreshInfo: false,
  // 企业主资料刷新充值记录列表
  etpRefreshTopLog: false
}

const mutations = {
  SET_AREA_SELECT(state, { data, key }) {
    state[key] = data
  },
  RESET_AREA_SELECT(state, keys) {
    keys.map(item => {
      for (const key in state) {
        if (item === key) {
          state[key] = []
        }
      }
    })
  },
  SET_TABS_TYPE(state, { value }) {
    if (value) {
      state.tab_key = value
    } else {
      state.tab_key = ''
    }
  },
  SET_ROLE_SELECT(state, data) {
    state.roleSelectList = data
  },
  SET_POINT_SELECT(state, { key, data }) {
    state.pointData[key] = data
  },
  RESET_POINT_SELECT(state) {
    state.pointData = {
      sectionName: '',
      goodsTable: [],
      classifyTable: []
    }
  },
  // 企业主资料刷新开票记录列表刷新写入
  SET_INVOCIE_LOG(state, data) {
    state.etpRefreshInvocieLog = data
  },
  // 企业主资料刷新基础信息刷新写入
  SET_INFO_REFRESH(state, data) {
    state.etpRefreshInfo = data
  },
  // 企业主资料刷新充值记录列表刷新写入
  SET_TOP_REFRESH(state, data) {
    state.etpRefreshTopLog = data
  },
  // 修改请求loading状态
  CHANGE_APPENDING(state, data) {
    state.isAppending = data
  }
}

const actions = {
  // 获取地区
  getAreaSelect({ commit }, { parentId = 100000, key }) {
    return new Promise((reslove, reject) => {
      commons
        .getArea(parentId)
        .then(response => {
          const { data } = response
          commit('SET_AREA_SELECT', { data, key })
          reslove(data)
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  // 获取角色下拉列表
  getRoleSelect({ commit }) {
    return new Promise((reslove, reject) => {
      system.role
        .getSelect()
        .then(response => {
          const { data } = response
          commit('SET_ROLE_SELECT', data)
          reslove(data)
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  // 获取交易设置
  getDealSet({ commit }) {
    return new Promise((reslove, reject) => {
      system.deal
        .getSet()
        .then(response => {
          const { data } = response
          commit('SET_DEAL_SET', data)
          reslove(data)
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  // 获取企业主信息
  getEtpInfo({ commit }, id) {
    return new Promise((resolve, reject) => {
      user.company
        .getInfo(id)
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
